<template>
  <el-tooltip
    effect="dark"
    :content="
      isFullscreen ? $t('common.outFullScreen') : $t('common.fullScreen')
    "
    placement="top"
  >
    <el-link
      :icon="`icon-ym ${
        isFullscreen ? 'icon-ym-compress-screen' : 'icon-ym-full-screen'
      } jsbos-common-head-icon`"
      :underline="false"
      @click="click()"
    />
  </el-tooltip>
</template>

<script>
import ScreenFull from "screenfull";

export default {
  props: {
    isContainer: {
      type: Boolean,
      default: true,
    },
  },
  name: "ScreenFull",
  data() {
    return {
      refEle: "",
      isFullscreen: false,
    };
  },
  mounted() {
    this.init();
    this.refEle = this.$options.parent.$el;
  },
  beforeDestroy() {
    this.destroy();
  },
  methods: {
    click() {
      if (!ScreenFull.enabled) {
        this.$message({
          message: "不支持全屏",
          type: "warning",
        });
        return false;
      }
      if (this.isContainer) {
        ScreenFull.toggle(this.refEle);
      } else {
        ScreenFull.toggle();
      }
    },
    change() {
      this.isFullscreen = ScreenFull.isFullscreen;
    },
    init() {
      if (ScreenFull.enabled) {
        ScreenFull.on("change", this.change);
      }
    },
    destroy() {
      if (ScreenFull.enabled) {
        ScreenFull.off("change", this.change);
      }
    },
  },
};
</script>
